package com.dlc.shop.systemtest.data.amount;


import cn.hutool.core.collection.CollUtil;
import com.dlc.shop.bean.model.Category;
import com.dlc.shop.bean.param.CategoryScoreConfigParam;
import com.dlc.shop.bean.param.ScoreConfigParam;
import com.dlc.shop.systemtest.api.amount.CategoryApi;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lhd
 */
public class ScoreData {

    public static ScoreConfigParam getScoreConfig(Double shopUseScore, Double useDiscount){
        ScoreConfigParam scoreConfigParam = new ScoreConfigParam();
        addCategoryConfig(scoreConfigParam, CategoryApi.listCategoryByGrade());
        scoreConfigParam.setSignInScore(Arrays.asList(1,2,3,4,5,6,7));
        scoreConfigParam.setRegisterScore(1L);
        scoreConfigParam.setShopGetScore(25.0);
        scoreConfigParam.setShopUseScore(shopUseScore);
        scoreConfigParam.setUseDiscount(useDiscount);
        scoreConfigParam.setUseDiscountRange(0);
        scoreConfigParam.setGetDiscountRange(0);
        return scoreConfigParam;
    }

    private static void addCategoryConfig(ScoreConfigParam scoreParam,List<Category> categories) {
        List<CategoryScoreConfigParam> params = new ArrayList<>();
        Map<Long, CategoryScoreConfigParam> categoryMap;
        if (CollUtil.isNotEmpty(scoreParam.getCategoryConfigs())) {
            categoryMap = scoreParam.getCategoryConfigs().stream().collect(Collectors.toMap(CategoryScoreConfigParam::getCategoryId, c->c));
        } else {
            categoryMap = new HashMap<>(0);
        }
        for (Category category : categories) {
            CategoryScoreConfigParam param = categoryMap.get(category.getCategoryId());
            if (Objects.isNull(param)) {
                param = new CategoryScoreConfigParam();
                param.setCategoryId(category.getCategoryId());
                param.setGetScoreLimit(1.0);
                param.setUseScoreLimit(1.0);
            }
            param.setCategoryName(category.getCategoryName());
            params.add(param);
        }
        scoreParam.setCategoryConfigs(params);
    }
}
